/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.wizard;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComponent;
import org.springframework.binding.value.ValueModel;
import org.springframework.binding.value.support.ValueHolder;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.FormModelHelper;
import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
import org.springframework.richclient.form.builder.HtmlFormBuilder;
import org.springframework.richclient.form.builder.TableFormBuilder;
import org.springframework.richclient.selection.binding.ListSelectionDialogBinder;
import org.springframework.richclient.selection.binding.support.LabelProvider;
import reportcenter.datasource.dominio.DataSource;
import reportcenter.datasource.business.DataSourceDao;
import reportcenter.report.dominio.report.Report;
import reportcenter.support.custom.binding.FileBinding;

/**
 *
 * @author renan_luz
 */
public class AdminReportFormW extends AbstractForm {

    private JComponent titleField;

    public AdminReportFormW(Report report) {
        super(report);
        
//        super(FormModelHelper.createFormModel(new ObjectExample()));
        setId("adminReportForm");
    }

    @Override
    protected JComponent createFormControl() {
        TableFormBuilder formBuilder = new TableFormBuilder(getBindingFactory());

        formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");

        formBuilder.addSeparator(getMessage("adminReportForm.separator.title1"));
        formBuilder.row();
        titleField = formBuilder.add("title")[1];
        formBuilder.add("subtitle");
        formBuilder.row();
        formBuilder.addTextArea("query");
        formBuilder.row();
        formBuilder.row();
        formBuilder.addSeparator("Intervalo de horário permitido para geração do relatório");
        formBuilder.row();
        formBuilder.add("horaIni");
        formBuilder.add("horaFim");
        formBuilder.row();
        
        //Criar bind em formato de lista para o atributo idD(DataSource)
        SwingBindingFactory f = new SwingBindingFactory(getFormModel());
        List<DataSource> listDS = ((DataSourceDao) getApplicationContext().getBean("dataSoureDao")).getAll();
        ValueModel listDSVm = new ValueHolder(listDS);

        Map m = new HashMap();
        m.put(ListSelectionDialogBinder.SELECTABLE_ITEMS_HOLDER_KEY, listDSVm);
        m.put(ListSelectionDialogBinder.LABEL_PROVIDER_KEY, new LabelProvider() {

            public String getLabel(Object item) {
                DataSource bean = (DataSource) item;
                return bean == null ? "" : bean.getName();
            }
        });
        
        m.put(ListSelectionDialogBinder.TITLE_KEY_KEY, "Escolha a Fonte de Dados");
        m.put(ListSelectionDialogBinder.FILTERED_KEY, Boolean.TRUE);
        m.put(ListSelectionDialogBinder.DESCRIPTION_KEY_KEY, "Filtro(Nome/URI/Usuário)");
        m.put(ListSelectionDialogBinder.FILTER_PROPERTIES_KEY,
                                             new String[] { "name","uri","usu", });

        formBuilder.add(getBindingFactory().createBinding("idD", m),"colSpan=1");
        
//         FileBinding binding = new FileBinding(getFormModel(), "myFileField");  
//         formBuilder.add(binding);  
        

        return formBuilder.getForm();
    }

    public boolean requestFocusInWindow() {
        return titleField.requestFocusInWindow();
    }
}
